<?php
// +----------------------------------------------------------------------
// | Fengli [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.yanyongwang.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 风黎 < 1280246895@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use app\common\model\QuestionBank;
use app\common\model\Questions;
use \think\Cache;
use \think\Controller;
use think\Loader;
use think\Db;
use \think\Cookie;
use app\admin\controller\Permissions;
use think\Validate;

/**问题列表
 * Class Ques
 * @package app\admin\controller
 */
class Ques extends Permissions
{
    public function index(){
        if(!request()->has('id'))       $this->error('参数错误');
        $id                     = request()->param('id');
        $model                  = new \app\common\model\Questions();

        $where                  = array(
            'qb_id'             => $id,
        );
        $param                  = request()->param();
        if(request()->has('question') && !empty($param['question'])){
            $where['question']     = ['like',"%{$param['question']}%"];
        }
        $quess                = !count($where) ? $model->where($where)->order('id desc')->paginate(config('paginate.list_rows'))
            : $model->where($where)->order('id desc')->paginate(config('paginate.list_rows'),false,['query'=>$param]);
        $this->assign('quess',$quess);
        $this->assign('qb_id',$id);
        return $this->fetch();
    }

    /**
     * 新增 or 编辑
     */
    public function addSave()
    {
        $model                  = new Questions();
        if($this->request->isPost()){
            $post               = request()->post();


            $id                 = request()->has('id') ? intval(request()->post('id')) : 0;


            //验证数据
            $validate           = new Validate([
                ['question','require','题库名称不能为空'],
                ['A','require','A答案不能为空'],
                ['B','require','B答案不能为空'],
                ['C','require','C答案不能为空'],
                ['D','require','D答案不能为空'],
                ['answer','require','正确答案不能为空'],
            ]);
            if(!$validate->check($post))
            {
                $this->error('数据格式错误:' . $validate->getError());
            }

            //防止入库出错 验证入库数据
            $saveData           = [
                'question'      => isset($post['question']) ? $post['question'] : '',
                'A'             => isset($post['A']) ? $post['A'] : '',
                'B'             => isset($post['B']) ? $post['B'] : '',
                'C'             => isset($post['C']) ? $post['C'] : '',
                'D'             => isset($post['D']) ? $post['D'] : '',
                'answer'        => isset($post['answer']) ? $post['answer'] : '',
                'qb_id'         => isset($post['qb_id']) ? $post['qb_id'] : '',

            ];
            if($id > 0){
                //如果ID 大于0编辑
                $key            = $model->allowField(true)->isUpdate(true)->save($saveData,['id'=>$id]);
                $message        = "编辑题目";
            }else{
                //新增菜单
                $key            = $model->allowField(true)->isUpdate(false)->save($saveData);
                $message        = "新增题目";
            }

            if($key){

                $this->success($message . '成功',url('admin/ques/index',['id'=>$post['qb_id']]) );
                //删除旧题库
            }else{
                $this->error($message . '失败');
            }

        }

        //如果有id则证明是编辑
        if(request()->has('id'))
        {
            $question          = $model ->where('id',request()->param('id'))->find();

            $this->assign('question',$question);
            $this->assign('qb_id',$question['qb_id']);
        }
        if(request()->has('qb_id')){
            $this->assign('qb_id',request()->param('qb_id'));
        }

        return $this->fetch();
    }

    /**
     * 题库删除
     */
    public function delete()
    {
        if($this->request->isPost()){
            $post = $this->request->post();

            $questionsModel     = new Questions();
            $id                 = $questionsModel->where(['id'=>$post['id']])->value('qb_id');
            $ids      = $questionsModel->where(['id'=>$post['id']])->delete();


            return $this->success('删除成功',url('admin/ques/index',['id'=>$id]));

        }
    }
}